package com.wjx.dingcan.web;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kzw.core.Constants;
import com.kzw.core.mapper.Jackson;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.util.ContextUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.util.StringUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.byxt.model.CheckMember;
import com.kzw.oa.byxt.model.Subject;
import com.kzw.oa.byxt.model.SubjectOpinion;
import com.kzw.oa.byxt.service.CheckMemberService;
import com.kzw.oa.byxt.service.SubjectOpinionService;
import com.kzw.oa.byxt.service.SubjectService;
import com.kzw.oa.byxt.service.YuXuanService;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.service.ext.AppUserService2;
import com.kzw.oa.util.JSON;
import com.kzw.oa.util.JSON2;
import com.wjx.dingcan.model.Card;
import com.wjx.dingcan.model.Food;
import com.wjx.dingcan.model.Order;
import com.wjx.dingcan.service.CardService;
import com.wjx.dingcan.service.OrderService;

@Controller
@Scope("prototype")
public class OrderAction extends BaseAction {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private CardService cardService;
	
	/**
	 * 显示列表
	 */
	public String list() {
		HttpServletRequest request = getRequest();
		
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		filters.add(new StringPropertyFilter("EQL_orderCustomer.userId", ContextUtil.getCurrentUser().getUserId()+""));
		Page<Order> page = orderService.search2(pageRequest, filters);

		jsonString = new JSON(page).buildWithFilters(3);
		return SUCCESS;
	}
	
	/**
	 * 增加订单
	 * 不要忘记删除购物车中的记录
	 * */
	public String add() {
		HttpServletRequest request = getRequest();
		//删除购物车中的记录
		Card card = cardService.findUniqueBy("owner", ContextUtil.getCurrentUser());
		String ids = request.getParameter("ids");
		
		if(ids == null || ids.equals("")) {
			return "{\"success\":false,\"msg\":\"没有选择相应的食物\"}";
		}
		
		Set<AppUser> shangjias = new HashSet<>();
		
		Set<Food> foods = card.getFoods();
		String[] idsArr = ids.trim().split(",");
		for(String oid : idsArr) {
			Iterator<Food> iterator = foods.iterator();
			while(iterator.hasNext()) {
				Food food = iterator.next();
				
				shangjias.add(food.getOwner());
				
				if(food.getOid().toString().equals(oid.trim())) {
					iterator.remove();
				}
			}
		}
		
		cardService.saveOrUpdate(card);
		
		Order order= new Order();
		
		Calendar calendar = Calendar.getInstance();
		int year = calendar.getWeekYear();
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		
		String orderCode = year + "" + month + "" + day + "" + hour + "" + minute; 
		
		String address  = request.getParameter("address");
		float orderPrice = Float.parseFloat(request.getParameter("orderPrice"));
		int orderStatus =  1;
		
		AppUser user = ContextUtil.getCurrentUser();
		
		order.setOrderCode(orderCode);
		order.setOrderPrice(orderPrice);
		order.setOrderStatus(orderStatus);
		order.setOrderTime(new Date());
		order.setOrderCustomer(user);
		order.setAddress(address);
		order.setShangjias(shangjias);
		
		orderService.saveOrUpdate(order);
		
		return SUCCESS;
	}
	
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		System.out.println(calendar.getWeekYear());
		System.out.println(calendar.get(Calendar.MONTH) + 1);
		System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
		System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println(calendar.get(Calendar.MINUTE));
	}


}
